/**
 * Copyright (C) 2008 ChangSha WangWin Science & Technology CO,.LTD. All rights reserved.
 * WangWin PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

/**
 * Copyright (C) 2002-2008 WUZEWEN. All rights reserved.
 * WUZEWEN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 * This software is the confidential and proprietary information of
 * WuZeven, Personal. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with WuZeven.
 */

package com.kingcore.pda.database.bean.impl;

import java.util.HashMap;

import org.apache.log4j.Logger;

import com.kingcore.pda.database.bean.DatabaseDictionary;

/**
 * <p>Oracle 数据字典对象。</p>
 * @author Zeven on 2008-11-25
 * @version	1.0
 * @see		Object#equals(java.lang.Object)
 * @see		Object#hashCode()
 * @see		HashMap
 * @since	JDK5
 */

public class OracleDatabaseDictionary implements DatabaseDictionary {

	private final static Logger log = Logger.getLogger(OracleDatabaseDictionary.class);
	
	/**
	 * <p>java方法的说明...</p>
	 * @author Zeven on 2008-11-25
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

	public String createSqlOfTableInforByCondition(String tableCondition) {
		StringBuffer sbf=new StringBuffer();
		//sbf.delete(0, sbf.length());
		sbf.append(" Select Table_Name, Table_Type, Comments")
			.append(" From User_Tab_Comments t")
			.append(" Where ("+tableCondition+") Or t.Table_Name = 'DEMOTABLE'")
			.append(" Order By t.Table_Name");
		log.debug(sbf);
		//String sql=" select TABLE_NAME,TABLE_TYPE,COMMENTS from user_tab_comments t where ("+tableCondition+") or t.TABLE_NAME='DEMOTABLE' order by t.TABLE_NAME " ;
		return sbf.toString();
		
	}

	public String createSqlOfTableInforByName(String dbName, String tableName ) {
		StringBuffer sbf=new StringBuffer();
		//sbf.delete(0, sbf.length());
		sbf.append(" Select Table_Name, Table_Type, Comments")
			.append(" From User_Tab_Comments t")
			.append(" Where t.Table_Name = '"+tableName.toUpperCase()+"'")
			.append(" Order By t.Table_Name");
		log.debug(sbf);
		//String sql=" select TABLE_NAME,TABLE_TYPE,COMMENTS from user_tab_comments t where t.TABLE_NAME='"+tableName.toUpperCase()+"' order by t.TABLE_NAME " ;
		return sbf.toString();
	}

	public String createSqlOfColumnInforByCondition(String dbName, String tableId, String tableEn) {
		StringBuffer sbf=new StringBuffer();
		//sbf.delete(0, sbf.length());
		sbf.append(" Select -1 As Isautoincrement, t.Column_Name, t.Data_Type, t.Data_Length, t.Data_Precision, t.Data_Scale, t.Nullable,")
			.append(" t.Column_Id, T2.Comments,")
			.append(" Decode(t.Data_Type, 'DATE', 'DATE', 'VARCHAR2', t.Data_Type || '(' || t.Data_Length || ')', 'VARCHAR',")
			.append(" t.Data_Type || '(' || t.Data_Length || ')', 'CHAR', t.Data_Type || '(' || t.Data_Length || ')', 'NUMBER',")
			.append(" t.Data_Type ||")
			.append(" (Nvl2(t.Data_Precision,")
			.append(" Nvl2(Decode(t.Data_Scale, 0, Null, t.Data_Scale),")
			.append(" '(' || t.Data_Precision || ',' || t.Data_Scale || ')', '(' || t.Data_Precision || ')'), '')),")
			.append(" t.Data_Type) Typefull")
			.append(" From User_Tab_Columns t, User_Col_Comments T2")
			.append(" Where t.Table_Name = T2.Table_Name")
			.append(" And t.Column_Name = T2.Column_Name")
			.append(" And t.Table_Name = '"+tableEn+"'")
			.append(" Order By t.Column_Id");
		log.debug(sbf);
		//return "select -1 as isAutoIncrement,t.COLUMN_NAME,t.DATA_TYPE,t.DATA_LENGTH,t.DATA_PRECISION,t.DATA_SCALE,t.NULLABLE,t.COLUMN_ID,t2.comments,decode(t.DATA_TYPE,'DATE','DATE','VARCHAR2',t.DATA_TYPE || '(' || t.DATA_LENGTH || ')','VARCHAR',t.DATA_TYPE || '(' || t.DATA_LENGTH || ')','CHAR',t.DATA_TYPE || '(' || t.DATA_LENGTH || ')','NUMBER',t.DATA_TYPE || (nvl2(t.DATA_PRECISION,nvl2(decode(t.DATA_SCALE,0,null,t.DATA_SCALE),'(' || t.DATA_PRECISION || ',' || t.DATA_SCALE || ')','(' || t.DATA_PRECISION || ')'),'')),t.DATA_TYPE) typefull from user_tab_columns t, user_col_comments t2 where t.TABLE_NAME = t2.table_name and t.COLUMN_NAME = t2.column_name and t.TABLE_NAME = '"+tableEn+"' order by t.column_id" ;
		return sbf.toString();
	}

	public String createSqlOfConstraintColumns(String dbName, String is_tblname, String constraint_name) {
		return null;
	}

	public String createSqlOfConstraints(String dbName, String tableEn) {
		return null;
	}

	/**
	 * like comment on column DemoTable.ID		is '序号';
	 */
	public String createUpdateCommentSql(String dbName, String tableEn, String colEn, String comment) {
		StringBuffer sbf = new StringBuffer();
		sbf.append("comment on column "+tableEn.trim()+"."+colEn + " is '"+comment+"'") ;
		log.debug(sbf);
		//return "select -1 as isAutoIncrement,t.COLUMN_NAME,t.DATA_TYPE,t.DATA_LENGTH,t.DATA_PRECISION,t.DATA_SCALE,t.NULLABLE,t.COLUMN_ID,t2.comments,decode(t.DATA_TYPE,'DATE','DATE','VARCHAR2',t.DATA_TYPE || '(' || t.DATA_LENGTH || ')','VARCHAR',t.DATA_TYPE || '(' || t.DATA_LENGTH || ')','CHAR',t.DATA_TYPE || '(' || t.DATA_LENGTH || ')','NUMBER',t.DATA_TYPE || (nvl2(t.DATA_PRECISION,nvl2(decode(t.DATA_SCALE,0,null,t.DATA_SCALE),'(' || t.DATA_PRECISION || ',' || t.DATA_SCALE || ')','(' || t.DATA_PRECISION || ')'),'')),t.DATA_TYPE) typefull from user_tab_columns t, user_col_comments t2 where t.TABLE_NAME = t2.table_name and t.COLUMN_NAME = t2.column_name and t.TABLE_NAME = '"+tableEn+"' order by t.column_id" ;
		return sbf.toString();
	}

}
